package com.jhgsys.internal.workflow.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.workflow.entity.WorkflowNodeButton;
import com.jhgsys.internal.workflow.mapper.WorkflowNodeButtonMapper;
import com.jhgsys.internal.workflow.service.IWorkflowNodeButtonService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 流程节点按钮 Service实现
 *
 * @author fenghaijun
 * @date 2024-07-19 09:18:06
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkflowNodeButtonServiceImpl extends ServiceImpl<WorkflowNodeButtonMapper, WorkflowNodeButton> implements IWorkflowNodeButtonService {

    /**
     * 查询（分页）
     * @author fenghaijun
     * @date 2024-07-19 09:18:06
     * @param request QueryRequest
     * @param workflowNodeButton workflowNodeButton
     * @return IPage<WorkflowNodeButton>
    */
    @Override
    public IPage<WorkflowNodeButton> findWorkflowNodeButtons(QueryRequest request, WorkflowNodeButton workflowNodeButton) {
        LambdaQueryWrapper<WorkflowNodeButton> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeButton::getDeletedFlg, FebsConstant.VALID);
        // TODO 设置查询条件

        Page<WorkflowNodeButton> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author fenghaijun
     * @date 2024-07-19 09:18:06
     * @param workflowNodeButton workflowNodeButton
     * @return List<WorkflowNodeButton>
     */
    @Override
    public List<WorkflowNodeButton> findWorkflowNodeButtons(WorkflowNodeButton workflowNodeButton) {
	    LambdaQueryWrapper<WorkflowNodeButton> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeButton::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(workflowNodeButton.getTenantId())){
            queryWrapper.eq(WorkflowNodeButton::getTenantId, workflowNodeButton.getTenantId());
        }
		if(StringUtils.isNotBlank(workflowNodeButton.getWorkflowId())){
            queryWrapper.eq(WorkflowNodeButton::getWorkflowId, workflowNodeButton.getWorkflowId());
        }
        if(StringUtils.isNotBlank(workflowNodeButton.getWorkflowNodeId())){
            queryWrapper.eq(WorkflowNodeButton::getWorkflowNodeId, workflowNodeButton.getWorkflowNodeId());
        }
        if(StringUtils.isNotBlank(workflowNodeButton.getNodeButtonId())){
            queryWrapper.eq(WorkflowNodeButton::getNodeButtonId, workflowNodeButton.getNodeButtonId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据条件查询
     * @author fenghaijun
     * @date 2024-07-19 09:18:06
     * @param workflowNodeButton workflowNodeButton
     * @return WorkflowNodeButton
     */
    @Override
    public WorkflowNodeButton findWorkflowNodeButton(WorkflowNodeButton workflowNodeButton) {
        LambdaQueryWrapper<WorkflowNodeButton> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeButton::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(workflowNodeButton.getTenantId())){
            queryWrapper.eq(WorkflowNodeButton::getTenantId, workflowNodeButton.getTenantId());
        }
        if(StringUtils.isNotBlank(workflowNodeButton.getWorkflowId())){
            queryWrapper.eq(WorkflowNodeButton::getWorkflowId, workflowNodeButton.getWorkflowId());
        }
        if(StringUtils.isNotBlank(workflowNodeButton.getWorkflowNodeId())){
            queryWrapper.eq(WorkflowNodeButton::getWorkflowNodeId, workflowNodeButton.getWorkflowNodeId());
        }
        if(StringUtils.isNotBlank(workflowNodeButton.getNodeButtonId())){
            queryWrapper.eq(WorkflowNodeButton::getNodeButtonId, workflowNodeButton.getNodeButtonId());
        }
        queryWrapper.last(" LIMIT 1 ");
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author fenghaijun
    * @date 2024-07-19 09:18:06
    * @param id
    * @return WorkflowNodeButton
    */
    @Override
    public WorkflowNodeButton findWorkflowNodeButtonById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author fenghaijun
     * @date 2024-07-19 09:18:06
     * @param workflowNodeButton workflowNodeButton
     */
    @Override
    @Transactional
    public void createWorkflowNodeButton(WorkflowNodeButton workflowNodeButton) {
        workflowNodeButton.setCreatedTime(new Date());
        this.save(workflowNodeButton);
    }

    /**
     * 修改
     * @author fenghaijun
     * @date 2024-07-19 09:18:06
     * @param workflowNodeButton workflowNodeButton
     */
    @Override
    @Transactional
    public void updateWorkflowNodeButton(WorkflowNodeButton workflowNodeButton) {
        workflowNodeButton.setUpdatedTime(new Date());
        this.saveOrUpdate(workflowNodeButton);
    }

    /**
     * 逻辑删除
     * @author fenghaijun
     * @date 2024-07-19 09:18:06
     * @param workflowNodeButton workflowNodeButton
     * @param workflowNodeButtonIds workflowNodeButtonIds
     */
    @Override
    @Transactional
    public void deleteWorkflowNodeButton(WorkflowNodeButton workflowNodeButton,String workflowNodeButtonIds) {
        List<String> list = Arrays.asList(workflowNodeButtonIds.split(StringPool.COMMA));
        this.baseMapper.update(workflowNodeButton,new LambdaQueryWrapper<WorkflowNodeButton>().in(WorkflowNodeButton::getNodeButtonId,list));
	}

    /**
    * 物理删除
    * @author fenghaijun
    * @date 2024-07-19 09:18:06
    * @param workflowNodeButtonIds workflowNodeButtonIds
    */
    @Override
    @Transactional
    public void physicallyDeleteWorkflowNodeButton(String workflowNodeButtonIds) {
        List<String> list = Arrays.asList(workflowNodeButtonIds.split(StringPool.COMMA));
        LambdaQueryWrapper<WorkflowNodeButton> wapper = new LambdaQueryWrapper<>();
        wapper.in(WorkflowNodeButton::getNodeButtonId,list);
        this.remove(wapper);
    }
}
